<?php

class BeersController extends ApplicationController
{
    protected $layout = 'scaffold';
    
    public function index()
    {
        $this->beers = Beer::$objects->all();
    }
    
    public function view()
    {
        $this->beer = Beer::$objects->get($this->params['id']);
    }
    
    public function create()
    {
        if (!$this->request->is_post())
        {
            $this->beer = new Beer();
        }
        else
        {
            $this->beer = new Beer($this->params['beer']);
            if ($this->beer->save())
            {
                $this->flash['notice'] = 'Beer was successfully created !';
                $this->redirect_to(array('action' => 'index'));
                return;
            }
        }
    }
    
    public function update()
    {
        if (!$this->request->is_post())
        {
            $this->beer = Beer::$objects->get($this->params['id']);
        }
        else
        {
            $this->beer = Beer::$objects->get($this->params['beer']['id']);
            if ($this->beer->update_attributes($this->params['beer']))
            {
                $this->flash['notice'] = 'Beer was successfully updated !';
                $this->redirect_to(array('action' => 'index'));
                return;
            }
        }
    }
    
    public function delete()
    {
        Beer::$objects->get($this->params['id'])->delete();
        $this->flash['notice'] = 'Beer was successfully deleted !';
        $this->redirect_to(array('action' => 'index'));
    }
}

?>
